import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
//首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard',affix:true }
    }]
  },
  //博客管理 
  {
    path: '/blog',
    component: Layout,
    redirect: '/blog/article',
    name: 'blog',
    meta: { title: '博客管理', icon: 'el-icon-s-help' },
    //一级菜单
    children: [
      {
        path: 'article',
        name: 'article',
        component: () => import('@/views/article/index'),
        meta: { title: '文章管理', icon: 'table' }
      },
      {
        path: 'category',
        name: 'category',
        component: () => import('@/views/category/index'),
        meta: { title: '分类管理', icon: 'tree' }
      },{
        path: 'label',
        name: 'Label',
        component: () => import('@/views/label/index'),
        meta: { title: '标签管理', icon: 'table' }
      }
    ]
  },
  //广告管理
  {
    path: '/advert',
    component: Layout,
    redirect: '/blog/article',
    name: 'blog',
    meta: { title: '博客管理', icon: 'el-icon-s-help' },
    //一级菜单
    children: [
      {
        path: 'index',
        name: 'advert',
        component: () => import('@/views/advert/index'),
        meta: { title: '广告管理', icon: 'form' }
      }
    ]
  },
  //系统管理
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'blog',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    //一级菜单
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/user/index'),
        meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/role/index'),
        meta: { title: '角色管理', icon: 'tree' }
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/menu/index'),
        meta: { title: '标签管理', icon: 'table' }
      }
    ]
  },
  //跳转官网（外网）
  {
    path:'study',
    component:Layout,
    children:[
      {
        path:'http://www.mengxuegu.com',
        meta:{title:'学习官网',icon:'link'}
      }
    ]
  },
  //标签导航刷新路由
  {
    path:'/redirect',
    component:Layout,
    hidden:true,
    children:[
      {
        path:"/redirect/:path(.*)"
      }
    ]
  },
  // 404页面
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // 默认是hash
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
